from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.uic import loadUiType
import sys
import dbutil
import pymainwindow

el, _ = loadUiType('editsellmation.ui')

class editsellmationApp(QMainWindow,el):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.changeButton.clicked.connect(lambda: self.edit_sell2())
        self.menu.triggered[QAction].connect(self.menuTriggered)

    def menuTriggered(self, q):
        if (q.text() == "返回上一页"):
            self.mainwindow_app = pymainwindow.MWApp()
            self.close()
            self.mainwindow_app.show()

        if (q.text() == "退出"):
            app = QApplication.instance()
            app.quit()
        return


    #修改销售信息
    def edit_sell2(self):
        num = self.numEdit.text()
        goodsname = self.goodsnameEdit.text()
        quantity = self.quantityEdit.text()
        quantityunit = self.quantityunitEdit.text()
        money = self.moneyEdit.text()
        staff = self.staffEdit.text()
        # 获取连接
        conn = dbutil.get_conn()
        # 获取cursor
        cur = conn.cursor()
        # SQL语句
        sql = "update sell set 商品名称=%s,销售数量=%s,数量单位=%s,金额=%s,销售员工=%s where 销售编号=%s"
        if (num=='' or goodsname=='' or quantity=='' or quantityunit=='' or money=='' or staff==''):
            QMessageBox.warning(self,"警告", "不能为空！", QMessageBox.Yes)
        else:
            warning = QMessageBox.warning(self, "提示", "您确定修改此条信息吗？", QMessageBox.Yes | QMessageBox.No)
            if warning == QMessageBox.Yes:
                # 执行语句
                cur.execute(sql, (goodsname,quantity,quantityunit,money,staff,num))
                # insert,update,delete必须显示提交
                conn.commit()
                # 关闭资源
                dbutil.close_conn(conn, cur)
                # 消息提示
                QMessageBox.information(self, "提示", "修改成功!",QMessageBox.Yes)
                self.numEdit.setText('')
                self.goodsnameEdit.setText('')
                self.quantityEdit.setText('')
                self.quantityunitEdit.setText('')
                self.moneyEdit.setText('')
                self.staffEdit.setText('')






def main():
    app = QApplication([])     # 创建应用程序 创建一个QApplication，也就是你要开发的软件app
    window = editsellmationApp()         # 创建主窗口  创建一个MainAPP，用来装载你需要的各种组件、控件
    window.show()              # 显示窗口 执行QMainWindow的show()方法，显示这个QMainWindow
    app.exec_()                # 使用exit()或者点击关闭按钮退出QApplication


if __name__ == '__main__':
    main()
